﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoundMatcher.bean_classes;
using System.Data;

namespace SoundMatcher
{
    class SoundMatcher
    {

       public List<int> GetBestFit(double[] input){

           double[] segmentfreDiference = new double[input.Length - 1];

           for (int j = 0; j < (input.Length - 1); j++)//loop to calculate fre difference from segment frequency array
           {
               segmentfreDiference[j] = input[j + 1] - input[j];
           }



           string query = "SELECT  freq_difference, id, title FROM song";
         DataTable songsWithFrequencyDiffs=DB.GetDataTable(query);
         DataTableReader tableReader = songsWithFrequencyDiffs.CreateDataReader();


           Dictionary<int,double> matchingSongsWithAccuracy = new Dictionary<int,double>();
           List<double> accuracyList=new List<double>();
           List<int> matchingSongs = new List<int>();

        while(tableReader.Read()){

           int id= Convert.ToInt32(tableReader["id"]);
           string title = Convert.ToString(tableReader["title"]);
           string freDifferencesString = Convert.ToString(tableReader["freq_difference"]);

           int[] freDifferencesOfOriginal = freDifferencesString.Split(',').Select(n =>int.Parse(n)).ToArray();

           
               
                    int range = 0;
                    int noOfSimilaFre = 0;


                    for (int y = 0; y < segmentfreDiference.Length;y++ )
                    {

                        int f = Convert.ToInt32(segmentfreDiference[y]);

                        bool success=freDifferencesOfOriginal.Contains(f);
                        if (success)
                       {
                          
                               noOfSimilaFre++;
                         
                           

                       }
                       else
                       {
                          int k=-1*(range/2);

                           while (k <= (range/2))
                           {



                               if(k==0){
                                   k++;
                                   continue;
                               }
                               bool freExist = freDifferencesOfOriginal.Contains(f + k);

                               if (freExist)
                               {

                                 
                                       noOfSimilaFre++;
                                
                               }
                               k++;
                               
                           }


                       }

                       


                    }


                    double accuracy = Convert.ToDouble( noOfSimilaFre) /Convert.ToDouble (segmentfreDiference.Length);

                
            //if(accuracy>.5){

                //Song s = new Song();
                //s.id = id;
                //s.title = title;

                matchingSongsWithAccuracy.Add(id, accuracy);
                accuracyList.Add(accuracy);

                
           // }

            
            }



        accuracyList.Sort();
        accuracyList.Reverse();

        foreach (double accuracy in accuracyList)
        {
            foreach (KeyValuePair<int,double> pair in matchingSongsWithAccuracy)
            {
                if (pair.Value == accuracy)
                {
                    if(!matchingSongs.Contains(pair.Key)){
                        matchingSongs.Add(pair.Key);

                    }
                    
                }
            }
        }

           return matchingSongs;
        }

    }
}
